#include "my_libproc.h"
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

int main()
{
    Cpu_Time* cpu_time[2];
    uint64_t count = 0, idx = 0;
    Cpu_Time* last_ptr, * now_ptr;
    if (init_cpu_time(&cpu_time[0]) == NULL || init_cpu_time(&cpu_time[1]) == NULL)
    {
        return -1;
    }
    int ret;
    // 初始化第一次读取 cpu_time[0]
    ret = get_cpu_time(cpu_time[0]);
    if (ret != 0)
    {
        perror("get_cpu_time failed");
        return ret;
    }
    last_ptr = NULL;
    now_ptr = cpu_time[0];

    while (1)
    {
        sleep(1);
        count++;
        idx = count % 2;

        ret = get_cpu_time(cpu_time[idx]);
        if (ret != 0)
        {
            perror("get_cpu_time failed");
            return ret;
        }
        last_ptr = now_ptr;
        now_ptr = cpu_time[idx];
        cal_cpu_usage(last_ptr, now_ptr);
        show_cpu_usage(now_ptr);
    }
    free_cpu_time(cpu_time[0]);
    free_cpu_time(cpu_time[1]);

    return 0;
}